package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseTeacherContraller {
    public Scanner sc = new Scanner(System.in);

    public final void teacherStart() {
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("输入有误,请重新输入:");
                    break;
            }
        }
    }

    private Socket getSocket() throws IOException {

        Socket socket = new Socket("127.0.0.1", 10086);
        return socket;
    }

    public final void updateTeacher() {
        try {
            Socket socket = getSocket();
            ArrayList<Teacher> allTeacher = findAllTeacher();
            if (allTeacher.isEmpty()) {
                System.out.println("系统暂无数据,请先添加");
            }
            String updateId = inputTeacherId("true");
            Teacher newTea = getTeacher(updateId);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[3]," + newTea.toString());
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            System.out.println(s);

            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    ;


    public final void deleteTeacher() {

        try {
            Socket socket = getSocket();
            ArrayList<Teacher> allTeacher = findAllTeacher();
            if (allTeacher.isEmpty()) {
                System.out.println("请先添加数据");
                return;
            }
            String delId = inputTeacherId("true");

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[2]," + delId);
            bw.flush();
            socket.shutdownOutput();


            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            System.out.println(s);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public final ArrayList<Teacher> findAllTeacher() {
        ArrayList<Teacher> list = null;
        try {
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[4]");
            bw.flush();
            bw.newLine();
            socket.shutdownOutput();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            list = (ArrayList<Teacher>) ois.readObject();
            System.out.println("----------------------------------------");
            System.out.println("工号\t\t" + "姓名\t" + "年龄\t" + "生日");
            System.out.println("----------------------------------------");
            for (int i = 0; i < list.size(); i++) {
                Teacher teacher = list.get(i);
                if (teacher != null) {
                    System.out.println(teacher.getSid() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
                }
            }
            System.out.println("----------------------------------------");
            //释放资源
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }



    public final void addTeacher() {
        try {
            Socket socket = getSocket();
            String sid = inputTeacherId("false");
            Teacher teacher = getTeacher(sid);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[1]," + teacher.toString());
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            socket.close();
            if (s.equals("true")) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public final String inputTeacherId(String b) {
        System.out.println("请输入工号:");
        String sid;
        while (true) {
            try {
                Socket socket = getSocket();
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                sid = sc.next();
                bw.write("[5]," + sid);
                bw.flush();
                socket.shutdownOutput();

                //获取服务器端返回的数据
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String res = br.readLine();
                if (b.equals(res)) {
                    break;
                } else {
                    System.out.println("工号存在,请重新输入:");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sid;
    }

    //获得老师信息,并封装为teacher对象
    public abstract Teacher getTeacher(String sid);
}
